﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.Windows;

namespace BDSim
{
    /// <summary>
    /// 星座数据交互模型
    /// </summary>
    public class ConstellationDataProvider : BDSimDataProvider
    {
        private Constellation _Parent;

        /// <summary>
        /// 数据模型的父级
        /// </summary>
        public Constellation Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }

        #region 命令定义
        
        /// <summary>
        /// 新建卫星命令
        /// </summary>
        MenuCommand AddSatelliteCmd = new MenuCommand();

        /// <summary>
        /// 保存星座命令
        /// </summary>
        MenuCommand SaveConstellationCmd = new MenuCommand();

   
        /// <summary>
        /// 清空星座中的卫星
        /// </summary>
        MenuCommand ClearSatelliteCmd = new MenuCommand();

        /// <summary>
        /// 删除该星座
        /// </summary>
        MenuCommand DeleteConstellationCmd = new MenuCommand();

        #endregion

        #region 命令集合初始化

        public override void DefineCommand()
        {
            AddSatelliteCmd.Header = "新建卫星";
            AddSatelliteCmd.DefineExcuteCommand(AddSatelliteFun_Execute,AddSatelliteFun_CanExecute);
            MenuCommandList.Add(AddSatelliteCmd);

            SaveConstellationCmd.Header = "保存星座";

            SaveConstellationCmd.DefineExcuteCommand(SaveConstellationFun_Execute, SaveConstellationFun_CanExecute);
            MenuCommandList.Add(SaveConstellationCmd);

            ClearSatelliteCmd.Header = "清空";
            ClearSatelliteCmd.DefineExcuteCommand(ClearSatelliteFun_Execute,ClearSatelliteFun_CanExecute);
            MenuCommandList.Add(ClearSatelliteCmd);

            DeleteConstellationCmd.Header = "删除";
            DeleteConstellationCmd.DefineExcuteCommand(DeleteConstellationFun_Execute, DeleteConstellationFun_CanExecute);
            MenuCommandList.Add(DeleteConstellationCmd);

        }

        #endregion

        #region 命令执行内容定义


        #region 新建卫星
        /// <summary>
        /// 新建卫星的执行方法
        /// </summary>
        void AddSatelliteFun_Execute(object param)
        {         
            Satellite Sat = new Satellite();
            Sat.Type = ESpaceType.Satellite;
            Sat.Id = Parent.SatelliteCollection.Count + 1;
            Sat.Name = NewSatName();
            Sat.Parent = this.Parent;
            Parent.SatelliteCollection.Add(Sat);
        }
        bool AddSatelliteFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        string NewSatName()
        {
            int count = Parent.Parent.AllSatellite.Count();
            string name = "Satellite" + (count + 1);
            bool flag = true;
            while (flag && count > 0)
            {
                name = "Satellite" + (count + 1);
                foreach (var sat in Parent.Parent.AllSatellite)
                {
                    if (sat.Name != name)
                    {
                        flag = false;
                        continue;
                    }
                    flag = true;
                    count++;
                    break;
                }
            }
            return name;
        }
        #endregion

        #region 保存星座
        /// <summary>
        /// 保存星座的执行方法
        /// </summary>
        void SaveConstellationFun_Execute(object param)
        {
            ConstellationXmlFile conData = new ConstellationXmlFile();
            conData.ConstellationData = this.Parent;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "星座文件|*.cst";
            if (sfd.ShowDialog() == true)
            {
                XmlFileOperater<ConstellationXmlFile>.SaveToFile(sfd.FileName, conData);
                MessageBox.Show("星座" + Parent.Name + "保存成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
               // BDSimObject.DlgReportPropertyInfo("星座" + Parent.Name + "保存成功!");
            }
        }

        bool SaveConstellationFun_CanExecute(object param)
        {
            bool flag = false;
            if ((this.Parent.SatelliteCollection.Count != 0) && (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End))
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 清空卫星
        /// <summary>
        /// 清空星座中的卫星的执行方法
        /// </summary>
        void ClearSatelliteFun_Execute(object param)
        {
            Parent.SatelliteCollection.Clear();
        }
        bool ClearSatelliteFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        #endregion

        #region 删除星座
        /// <summary>
        /// 删除星座的执行方法
        /// </summary>
        void DeleteConstellationFun_Execute(object param)
        {
            Parent.Parent.ConstellationList.Remove(Parent);
        }
        bool DeleteConstellationFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #endregion


    }
}
